package com.zunipe.accounts;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.zunipe.accounts.database.Result;
import com.zunipe.accounts.database.entity.User;
import com.zunipe.accounts.database.handler.AccountHandler;
import com.zunipe.accounts.database.handler.LoginHandler;
import com.zunipe.accounts.databinding.ActivityLoginBinding;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    ActivityLoginBinding binding;
    private LoginHandler loginHandler;
    private AccountHandler accountHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        loginHandler = new LoginHandler();
        accountHandler = new AccountHandler();
        binding.btnLogin.setOnClickListener(this);
        binding.btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_login) {
            String username = binding.etUsername.getText().toString();
            String password = binding.etPassword.getText().toString();

            Result result = loginHandler.tryLogin(new User(username, password));
            if (result.getCode() == Result.FAILED_CODE) {
                Toast.makeText(this, result.getMsg(), Toast.LENGTH_SHORT).show();
                return;
            }
            if (accountHandler.hasTouristDataAvailable()) {
                AlertDialog dialog = new AlertDialog.Builder(this)
                        .setTitle("提示")
                        .setMessage("是否保存游客数据？")
                        .setPositiveButton("是", (dialog1, which) -> {
                            accountHandler.saveTouristData(AccountApplication.getInstance().getUserId());
                            Intent intent = new Intent();
                            intent.setAction("com.zunipe.accounts.update.list");
                            intent.setPackage("com.zunipe.accounts");
                            sendBroadcast(intent);
                            finish();
                        })
                        .setNegativeButton("否", (dialog1, which) -> {
                            accountHandler.deleteTouristData();
                            Intent intent = new Intent();
                            intent.setAction("com.zunipe.accounts.update.list");
                            intent.setPackage("com.zunipe.accounts");
                            sendBroadcast(intent);
                            finish();
                        })
                        .create();
                dialog.show();
            } else {
                Toast.makeText(this, result.getMsg(), Toast.LENGTH_SHORT).show();
                finish();
            }
        } else if (id == R.id.btn_register) {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        }
    }
}